<?php
// +----------------------------------------------------------------------
// | HkCms 模块管理模型
// +----------------------------------------------------------------------
// | Copyright (c) 2012-2019 http://www.hkcms.cn, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 广州恒企教育科技有限公司 <admin@hkcms.cn>
// +----------------------------------------------------------------------

namespace common\model;

class Module extends Model
{
    protected $insert = ['iscore'=>0,'disabled'=>1,'installtime','updatetime'];

    public function setInstalltimeAttr()
    {
        return time();
    }

    public function setUpdatetimeAttr()
    {
        return time();
    }

    /**
     * 模块状态转换
     * @param $module
     * @return bool
     */
    public function disabled($module)
    {
        if (empty($module)) {
            $this->error = '请选模块！';
            return false;
        }
        //取得该模块数据库中记录的安装信息
        $info = $this->where(array('module' => $module))->find();
        if (empty($info)) {
            $this->error = '该模块未安装，无需进行此操作！';
            return false;
        }
        $info = $info->toArray();
        if ($info['iscore']) {
            $this->error = '内置模块，不能禁用！';
            return false;
        }
        $disabled = $info['disabled'] ? 0 : 1;
        if (false !== $this->save(array('disabled' => $disabled),array('module' => $module))) {
            //更新缓存
            cache('Module', NULL);
            return true;
        } else {
            $this->error = '状态转换失败！';
            return false;
        }
    }

    /**
     * 更新缓存
     * @return array | boolean
     */
    public function module_cache()
    {
        $data = $this->select()->toArray();
        if (empty($data)) {
            return false;
        }
        $module = array();
        foreach ($data as $v) {
            $module[$v['module']] = $v;
        }
        cache('Module', $module);
        return $module;
    }

}
